
	  
using Youz.Common.BLL;
using Youz.Common.Helper;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace Youz.CreditRepay.Manage.Handlers
{
    public partial class Log_ErrorLogHandler : IHttpHandler, IRequiresSessionState
    {
        Log_ErrorLogBLL bll = new Log_ErrorLogBLL();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string _type = WebHelper.Get("type");
            string result = string.Empty;
            switch (_type)
            {
                case "get":
                    result = Get();
                    break;
                case "list":
                    result = List();
                    break;
                case "save":
                    result = Save();
                    break;
                case "delete":
                    result = Delete();
                    break;
            }

            context.Response.Write(result);
            context.Response.End();
        }

        private string Get()
        {
            Guid ErrorLogID = WebHelper.Post<Guid>("ErrorLogID");

            return bll.GetJson(ErrorLogID);
        }

        private string List()
        {
            int pageIndex = WebHelper.Post<int>("start");
            int pageSize = WebHelper.Post<int>("length");

            string LoginType = WebHelper.Post("LoginType").Trim();
            string StartDate = WebHelper.Post("StartDate");
            string EndDate = WebHelper.Post("EndDate");

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" 1=1");


            if (!string.IsNullOrEmpty(LoginType))
            {
                sb.AppendFormat(" and LoginType = '{0}'", LoginType);
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                sb.AppendFormat(" and CreateTime >= '{0}'", StartDate);
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                EndDate = Convert.ToDateTime(EndDate).AddDays(1).ToString("yyyy-MM-dd");
                sb.AppendFormat(" and CreateTime < '{0}'", EndDate);
            }

            int total = 0;
            DataTable dt = bll.Gets(sb.ToString(), pageIndex, pageSize, out total);

            return Utility.GetJsonByDataTableHaveTime(total, dt);
        }

        private string Save()
        {
            string id = WebHelper.Post("ErrorLogID");
            bool isAdd = string.IsNullOrWhiteSpace(id);
            Log_ErrorLog model = new Log_ErrorLog();

            if (isAdd)
            {
            }
            else
            {
                model = bll.Get(WebHelper.Post<Guid>("ErrorLogID"));
            }

            model.Host = WebHelper.Post<String>("Host");
            model.RequestType = WebHelper.Post<String>("RequestType");
            model.LoginType = WebHelper.Post<Int32>("LoginType");
            model.AbsolutePath = WebHelper.Post<String>("AbsolutePath");
            model.PostData = WebHelper.Post<String>("PostData");
            model.QueryString = WebHelper.Post<String>("QueryString");
            model.UserHostAddress = WebHelper.Post<String>("UserHostAddress");
            model.UserAgent = WebHelper.Post<String>("UserAgent");
            model.Source = WebHelper.Post<String>("Source");
            model.TargetSite = WebHelper.Post<String>("TargetSite");
            model.ErrMess = WebHelper.Post<String>("ErrMess");
            model.BaseErrMess = WebHelper.Post<String>("BaseErrMess");
            model.StackTraceErr = WebHelper.Post<String>("StackTraceErr");
            model.CreateTime = WebHelper.Post<DateTime>("CreateTime");


            if (isAdd ? bll.Add(model) : bll.Update(model))
            {
                return Utility.AjaxResult(true, AjaxResultType.Save);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Save);
            }
        }

        private string Delete()
        {
            if (bll.Delete(WebHelper.Post<Guid>("ErrorLogID")))
            {
                return Utility.AjaxResult(true, AjaxResultType.Delete);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Delete);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

  